﻿/*
 * 2023-06-16 PconSendMsg新增字段Number，Response
 * Number为递增数
 * Response为设备处理完后需要跟Number相同
 */
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using System;
using System.Collections.Generic;
using System.Text;

namespace HamcoWcs.Models.DeviceMsgs
{
    public class PconRecvMsg : MsgBase
    {
        public ushort PconName { get; set; }
        public ushort PconMode { get; set; }
        public ushort ErrorCode { get; set; }
        public ushort IsGoods { get; set; }
        public ushort CheckRes { get; set; }
        public ushort Size { get; set; }
        public ushort CheckResult { get; set; }
        public ushort Weight { get; set; }
        public ushort DestLoc { get; set; }
        public string UnitId { get; set; } = "";
        public UInt32 DevLoc { get; set; }

        public ushort Speed { get; set; }

        public ushort WorkType { get; set; }
        public UInt32 Input { get; set; }
        public UInt32 Output { get; set; }
        public ushort Request { get; set; }
        public PconRecvMsg(byte[] msg)
        {
            //PconName = BitConverter.ToUInt16(GetBytes(msg,0,2));
            //PconMode = BitConverter.ToUInt16(GetBytes(msg, 2, 2));
            //ErrorCode = BitConverter.ToUInt16(GetBytes(msg, 4, 2));
            //IsGoods = BitConverter.ToUInt16(GetBytes(msg, 6, 2));
            //CheckRes = BitConverter.ToUInt16(GetBytes(msg, 6, 2));
            //Size = BitConverter.ToUInt16(GetBytes(msg, 8, 2));
            //CheckResult = BitConverter.ToUInt16(GetBytes(msg, 10, 2));
            //Weight = BitConverter.ToUInt16(GetBytes(msg, 12, 2));
            //DestLoc = BitConverter.ToUInt16(GetBytes(msg, 14, 2));
            //UnitId = Encoding.ASCII.GetString(msg, 16, 18).Trim().Trim('\0') ?? "";
            //DevLoc = BitConverter.ToUInt32(GetBytes(msg, 34, 4));
            //Speed = BitConverter.ToUInt32(GetBytes(msg, 38, 4));
            //WorkType = BitConverter.ToUInt16(GetBytes(msg, 42, 2));
            //Input = BitConverter.ToUInt16(GetBytes(msg, 44, 4));
            //Output = BitConverter.ToUInt16(GetBytes(msg, 48, 4));
            //Request = BitConverter.ToUInt16(GetBytes(msg, 52, 2));

            PconName = BitConverter.ToUInt16(GetBytes(msg, 0, 2));
            PconMode = BitConverter.ToUInt16(GetBytes(msg, 2, 2));
            ErrorCode = BitConverter.ToUInt16(GetBytes(msg, 4, 2));
            IsGoods = BitConverter.ToUInt16(GetBytes(msg, 6, 2));
            CheckRes = BitConverter.ToUInt16(GetBytes(msg, 8, 2));
            Size = BitConverter.ToUInt16(GetBytes(msg, 10, 2));
            CheckResult = BitConverter.ToUInt16(GetBytes(msg, 12, 2));
            Weight = BitConverter.ToUInt16(GetBytes(msg, 14, 2));
            DestLoc = BitConverter.ToUInt16(GetBytes(msg, 16, 2));
            UnitId = Encoding.ASCII.GetString(msg, 18, 18).Trim().Trim('\0') ?? "";
            DevLoc = BitConverter.ToUInt32(GetBytes(msg, 36, 4));
            Speed = BitConverter.ToUInt16(GetBytes(msg, 40, 2));
            WorkType = BitConverter.ToUInt16(GetBytes(msg, 42, 2));
            Input = BitConverter.ToUInt16(GetBytes(msg, 44, 4));
            Output = BitConverter.ToUInt16(GetBytes(msg, 48, 4));
            Request = BitConverter.ToUInt16(GetBytes(msg, 52, 2));
        }

        public PconRecvMsg() : base(true)
        {

        }
        public PconRecvMsg(bool isReserve = true) : base(isReserve)
        {
        }
    }
    public class PconSendMsg : MsgBase
    {
        public ushort PconName { get; set; }
        public ushort DestLoc { get; set; }
        public ushort Appearance { get; set; }
        public ushort Size { get; set; }
        public string UnitId { get; set; } = "";
        public ushort ErrorCode { get; set; }
        public ushort Request { get; set; }
        public ushort Number { get; set; }      //2023-06-16 新增
        public ushort Response { get; set; }    //2023-06-16 新增


        public static PconSendMsg CreateDestMsg(ushort pconName, ushort destLoc, string unitId)
            => new PconSendMsg()
            {
                PconName = pconName,
                DestLoc = destLoc,
                UnitId = unitId,
                Request = 1
            };

        public static PconSendMsg CreateInModeMsg(ushort pconName)
            => new PconSendMsg()
            {
                PconName = pconName,
                Request = 6
            };

        public static PconSendMsg CreateRejectChangeModeMsg(PconDto pcon)
            => new PconSendMsg()
            {
                PconName = (ushort)pcon.PconName,
                Request = (ushort)EnumPconSendMsg.RejectChangeMode
            };

        public PconSendMsg(bool isReserve = true) : base(isReserve)
        {
        }

        public PconSendMsg() : base(true)
        {

        }


        public PconSendMsg(byte[] msg, bool isReserve = true) : base(isReserve)
        {
            PconName = Reserve(BitConverter.ToUInt16(msg, 0));
            DestLoc = Reserve(BitConverter.ToUInt16(msg, 2));
            Appearance = Reserve(BitConverter.ToUInt16(msg, 4));
            Size = Reserve(BitConverter.ToUInt16(msg, 6));

            UnitId = Encoding.ASCII.GetString(msg, 8, 18).Trim().Trim('\0');
            ErrorCode = Reserve(BitConverter.ToUInt16(msg, 26));
            Request = Reserve(BitConverter.ToUInt16(msg, 28));
            Number = Reserve(BitConverter.ToUInt16(msg, 30));
            Response = Reserve(BitConverter.ToUInt16(msg, 32));
            if (UnitId == null)
                UnitId = "";
        }
        public byte[] Encode()
        {
            var bytes = new List<byte>();
            bytes.AddRange(BitConverter.GetBytes(Reserve(PconName)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(DestLoc)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Appearance)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Size)));
            bytes.AddRange(Encoding.ASCII.GetBytes(UnitId.PadRight(18, ' ')));
            bytes.AddRange(BitConverter.GetBytes(Reserve(ErrorCode)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Request)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Number)));
            bytes.AddRange(BitConverter.GetBytes(Reserve(Response)));
            return bytes.ToArray();
        }

    }
}
